package com.lottery.material.service.portal.circle.impl;

import com.lottery.material.dao.circle.CircleInfoMapper;
import com.lottery.material.dao.circle.CircleMemberInfoMapper;
import com.lottery.material.pojo.circle.CircleInfo;
import com.lottery.material.pojo.circle.CircleMemberInfo;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.service.portal.circle.CircleMemberInfoService;
import com.lottery.material.vo.portal.circle.CircleInfoVo;
import com.lottery.material.vo.portal.circle.MemberCircleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author:houjie
 * @Description: 圈成员接口实现类
 * @Date:Create in 2019/4/9 15:17
 */
@Service
@Slf4j
public class CircleMemberInfoServiceImpl implements CircleMemberInfoService {

    @Autowired
    private CircleMemberInfoMapper circleMemberInfoMapper;
    @Autowired
    private CircleInfoMapper circleInfoMapper;

    /**
    * @Author: houjie
    * @Description: 根据用户userId及圈子id获取圈成员信息
    * @Date: 2019/4/9 15:58
    */
    @Override
    public CircleMemberInfo findByUserIdAndCircleId(PageData pd) {
        return circleMemberInfoMapper.findByUserIdAndCircleId(pd);
    }

    /**
     * description: 删除圈子成员
     * author sunny
     * createDate 2019-04-15 下午 2:51
     * @param circleMemberInfo
     */
    @Override
    public Integer deleteCircleMember(CircleMemberInfo circleMemberInfo, CircleInfo circleInfo) throws Exception {
        PageData pd = new PageData();
        pd.put("userId", circleMemberInfo.getUserId());
        pd.put("circleId", circleMemberInfo.getCircleId());
        CircleMemberInfo circle = circleMemberInfoMapper.findByUserIdAndCircleId(pd);
        if(Objects.isNull(circle)){
            log.info("当前用户：{}-->还没有关注该圈子{}，无法取消关注", circleMemberInfo.getUserId(), circleMemberInfo.getCircleId());
            throw new Exception("未能查询到关注记录，所以无法取消关注！");
        }

        Integer count = circleMemberInfoMapper.deleteByPrimaryKey(circle.getId());
        if(count <= 0){
            throw new Exception("删除关注记录失败！");
        }
        circleInfo.setFansTotal(circleInfo.getFansTotal() == 0 ? 0 : circleInfo.getFansTotal() - 1);
        Integer updateCount = circleInfoMapper.updateByPrimaryKeySelective(circleInfo);
        if(updateCount <= 0){
            throw new Exception("圈子信息表粉丝数更新失败！");
        }
        return circleInfo.getFansTotal();
    }

    /**
     * description: 保存圈子成员
     * author sunny
     * createDate 2019-04-15 下午 3:04
     * @param circleMemberInfo
     */
    @Override
    public Integer saveCircleMember(CircleMemberInfo circleMemberInfo, CircleInfo circleInfo) throws Exception {
        PageData pd = new PageData();
        pd.put("userId", circleMemberInfo.getUserId());
        pd.put("circleId", circleMemberInfo.getCircleId());
        CircleMemberInfo circle = circleMemberInfoMapper.findByUserIdAndCircleId(pd);
        if(Objects.nonNull(circle)){
            log.info("当前用户：{}-->已经关注该圈子{}，无法再次关注", circleMemberInfo.getUserId(), circleMemberInfo.getCircleId());
            throw new Exception("圈子已经被关注，无法再次关注！");
        }

        Integer count = circleMemberInfoMapper.insertSelective(circleMemberInfo);
        if(count <= 0){
            throw new Exception("圈子成员信息表数据插入失败！");
        }

        circleInfo.setFansTotal(circleInfo.getFansTotal() + 1);
        Integer updateCount = circleInfoMapper.updateByPrimaryKeySelective(circleInfo);
        if(updateCount <= 0){
            throw new Exception("圈子信息表粉丝数更新失败！");
        }
        return circleInfo.getFansTotal();
    }

    @Override
    public List<MemberCircleVo> findMyCircleList(String userId) {
        Map<String ,Object> map = new HashMap<>();
        map.put("userId",userId);
        return circleMemberInfoMapper.findCircleListByUserId(map);
    }

    /**
     * 查询置顶圈子列表
     * @Author 焦阳
     * @param userId
     * @return
     */
    @Override
    public List<MemberCircleVo> findTopCircleList(String userId) {
        Map<String ,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("isTop",2);
        return circleMemberInfoMapper.findCircleListByUserId(map);
    }

    /**
     * 置顶，取消置顶圈子
     * @Author 焦阳
     * @param reqParamMap
     * @return
     */
    @Override
    public Integer setTopCircle(Map<String, String> reqParamMap) {
        PageData pd = new PageData();
        pd.put("userId", reqParamMap.get("userId"));
        pd.put("circleId", reqParamMap.get("circleId"));
        CircleMemberInfo circleMemberInfo = circleMemberInfoMapper.findByUserIdAndCircleId(pd);
        Integer isTop = Integer.parseInt(reqParamMap.get("isTop"));
        //未置顶时取消置顶
        if(isTop==1&isTop==circleMemberInfo.getIsTop()){
            return 3;
        }
        //已置顶设置置顶
        if(isTop==2&isTop==circleMemberInfo.getIsTop()){
            return 4;
        }
        circleMemberInfo.setIsTop(isTop);
        Integer i = circleMemberInfoMapper.updateByPrimaryKeySelective(circleMemberInfo);
        //置顶成功
        if(i==1){
            return isTop;
        }else{
            return 0; //置顶失败
        }
    }

    /**
     * description: 根据用户id查询关注圈子数
     * author sunny
     * createDate 2019-04-15 下午 4:18
     * @param userId
     * @return
     */
    @Override
    public Integer findCountByUserId(String userId) {
        return circleMemberInfoMapper.findCountByUserId(userId);
    }


    /**
     * @Author 肖拔赟
     * @Description 5.1	获取我的关注圈子列表接口
     * @Date Created on 2019-04-19 15:08
     * @param
     * @return CircleInfoVo
     */
    @Override
    public List<CircleInfoVo> findAttentionByUserId(String userId) {
        return circleMemberInfoMapper.findAttentionByUserId(userId);
    }


    /**
     * @Author 肖拔赟
     * @Description 保存圈子成员信息
     * @Date Created on 2019-04-19 15:08
     * @param
     * @return CircleInfoVo
     */
    @Override
    public Integer save(CircleMemberInfo record) {
        return circleMemberInfoMapper.insertSelective(record);
    }

    /**
     * @Author 肖拔赟
     * @Description 5.1	获取圈子粉丝Top10
     * @Date Created on 2019-04-19 15:08
     */
    @Override
    public List<Map<String, String>> getFansTop10(String circleId){
        return circleMemberInfoMapper.getFansTop10(circleId);
    }

}
